/*
 * $Id: thread.c,v 1.8 2005-12-15 07:19:12 bacon Exp $
 */

#include <xp/bas/thr.h>
#include <xp/bas/time.h>
#include <xp/bas/stdio.h>

int dummy (xp_thr_t* thr)
{
	int i;

	for (i = 0; i < 5; i++) {
		xp_printf (XP_TEXT("dummy. %i\n"), i);
		xp_sleep (1000);
	}

	return 0;
}

int dummy2 (xp_thr_t* thr)
{
	int i;

	for (i = 0; i < 5; i++) {
		xp_printf (XP_TEXT("dummy2. %i\n"), i);
		xp_sleep (1000);
	}

	return 0;
}

int xp_main ()
{
	xp_thr_t thr;

	if (xp_thr_open (&thr, dummy, 0) == XP_NULL) {
		xp_printf (XP_TEXT("cannot open thread\n"));
		return -1;
	}

	if (xp_thr_start (&thr, 0) == -1) {
		xp_printf (XP_TEXT("cannot start thread\n"));
		xp_thr_close (&thr);
		return -1;
	}
	xp_thr_join (&thr);

	xp_printf (XP_TEXT("-------------------\n"));
	if (xp_thr_start (&thr, XP_THR_NEW_ROUTINE, dummy2) == -1) {
		xp_printf (XP_TEXT("cannot start thread\n"));
		xp_thr_close (&thr);
		return -1;
	}
	xp_thr_join (&thr);

	xp_printf (XP_TEXT("-------------------\n"));
	if (xp_thr_start (&thr, 0) == -1) {
		xp_printf (XP_TEXT("cannot start thread\n"));
		xp_thr_close (&thr);
		return -1;
	}
	xp_thr_join (&thr);

	xp_thr_close (&thr);
	return 0;
}
